package com.wufun.overseas.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;


import com.wufun.overseas.R;
import com.wufun.overseas.view.adapter.AlbumAdapter;
import com.wufun.overseas.view.lib.ImageFolder;
import com.wufun.overseas.view.lib.ImageScaner;

import java.util.List;

/**
 * Created by xiet on 2015/7/8.
 */
public class AlbumPopupWindow extends PopupWindow {
    private final Context     mContext;
    private final View        parentView;
    private       ListView    mListView;
    private ImageScaner mScaner;
    private       int         mScreenHeight;

    public AlbumPopupWindow(Context context, View parent, int width, int height) {
        super(parent, width, height, true);
        mContext = context;
        parentView = parent;

        initWindownParams();
        setupAttrs();
        initViews();
    }

    private void initWindownParams() {
        DisplayMetrics outMetrics = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        mScreenHeight = outMetrics.heightPixels;
    }

    private void initViews() {
//        View parentView = LayoutInflater.from(this).inflate(R.layout.album_layout, null);
        mListView = (ListView) parentView.findViewById(R.id.list);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mListView.getLayoutParams();
        params.height = (int) (mScreenHeight * 0.6);

        /*AlbumScrollListener scrollListener = new AlbumScrollListener(ImageLoader.getInstance(), true, true);
        scrollListener.setScrollListener(new AlbumScrollListener.ScrollListener() {
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }
        });
        mListView.setOnScrollListener(scrollListener);*/


        mScaner = new ImageScaner(mContext);
        mScaner.setOnImageScanListener(new ImageScaner.OnImageScanListener() {
            @Override
            public void onScanStart() {

//                Log.e("setOnImageScanListener", "start scan...");
            }

            @Override
            public void onScaning(Object item) {

            }

            @Override
            public void onScanEnd(final Object result) {
                AlbumAdapter mAdapter = new AlbumAdapter(mContext, (List<ImageFolder>) result);
                mListView.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
                mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (mOnAlbumSelectedListener != null)
                            mOnAlbumSelectedListener.selected(((List<ImageFolder>) result).get(position));
                    }
                });

//                Log.e("setOnImageScanListener", "end scan...");
            }
        });

        init();
    }

    public void init() {
        new Thread() {
            @Override
            public void run() {
                super.run();

                mScaner.scan();
            }
        }.start();
    }


    private void setupAttrs() {
        setBackgroundDrawable(new BitmapDrawable());
        setTouchable(true);
        setOutsideTouchable(true);
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });
    }

    public interface OnAlbumSelectedListener {
        void selected(ImageFolder folder);
    }

    public static OnAlbumSelectedListener mOnAlbumSelectedListener;

    public void setOnAlbumSelectedListener(OnAlbumSelectedListener l) {
        AlbumPopupWindow.mOnAlbumSelectedListener = l;
    }

    /*public static class AlbumScrollListener extends PauseOnScrollListener {

        ScrollListener scrollListener;

        public AlbumScrollListener(ImageLoader imageLoader, boolean pauseOnScroll, boolean pauseOnFling) {
            super(imageLoader, pauseOnScroll, pauseOnFling);

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            super.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
            if (scrollListener != null)
                scrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            super.onScrollStateChanged(view, scrollState);
            if (scrollListener != null) scrollListener.onScrollStateChanged(view, scrollState);
        }

        public interface ScrollListener {
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount);

            public void onScrollStateChanged(AbsListView view, int scrollState);
        }

        public ScrollListener getScrollListener() {
            return scrollListener;
        }

        public void setScrollListener(ScrollListener scrollListener) {
            this.scrollListener = scrollListener;
        }
    }*/
}
